package com.threeox.commonlibrary.ui.activity.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.threeox.commonlibrary.CommonApplcation;
import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.sysbar.SystemBarTintManager;
import com.threeox.commonlibrary.ui.view.CommonTitleBarView;
import com.threeox.commonlibrary.ui.view.CommonTitleBarView.LayoutStyle;
import com.threeox.commonlibrary.ui.view.CommonTitleBarView.TopBarStyle;
import com.threeox.commonlibrary.ui.view.inter.titlebar.OnTopBarListener;
import com.threeox.commonlibrary.util.ActivityObjects;
import com.threeox.commonlibrary.util.ViewUtils;
import com.threeox.utillibrary.util.LogUtils;
import com.threeox.utillibrary.util.ToastUtils;
import com.threeox.utillibrary.util.res.LayoutParamsUtils;

import java.io.Serializable;

/**
 *
 * @ClassName: CommonActivity
 *
 * @Description: (基础的Activity)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/19 18:18
 *
 * @version 1.0
 */
@SuppressLint({ "InlinedApi", "NewApi" })
@SuppressWarnings("unused")
public abstract class CommonActivity extends FragmentActivity implements OnClickListener, OnTopBarListener {

	protected final String TAG = this.getClass().getName();

	protected Intent mIntent;
	protected Context mContext;
    protected ViewUtils mViewUtils;
    protected CommonApplcation mApplication;
	protected CommonTitleBarView mTitleBarView;
	protected SystemBarTintManager mTintManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			onCreate();
			setView(savedInstanceState);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
				setTranslucentStatus(true);
			}
			mTintManager = new SystemBarTintManager(this);
			mTintManager.setStatusBarTintEnabled(true);// 启用系统状态栏的着色
			mTintManager.setNavigationBarTintEnabled(true);// 启用系统导航栏的着色
			mTintManager.setStatusBarTintResource(R.color.statusBarColor);// 通知栏所需颜色
			initView();
			initData();
			setListener();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// getWindow().setSoftInputMode(
		// WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}

	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}

	/**
	 * 设置背景
	 * 
	 * @param drawId
	 */
	protected void setBackGround(int drawId) {
		getWindow().setBackgroundDrawableResource(drawId);
	}

	protected void onCreate() {
		this.mContext = this;
		this.mIntent = getIntent();
        this.mViewUtils = ViewUtils.newInstance();
        ActivityObjects.getInstance().putActivity(this);
        this.mApplication = CommonApplcation.getInstance();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 竖屏
    }

	/** 设置View */
	protected abstract void setView(Bundle stateBundle);

	/** 初始化布局 */
	protected abstract void initView();

	/** 初始化数据 */
	protected abstract void initData();

	/** 设置监听器 */
	protected abstract void setListener();

	/**
	 * 初始化只有标题
	 * 
	 * @param tbs
	 * @param title
	 */
	protected void initTitle(TopBarStyle tbs, CharSequence title) {
		mTitleBarView = findViewById(R.id.id_topbar_view);
		if (mTitleBarView == null) {
            mTitleBarView = new CommonTitleBarView(mContext);
			android.widget.LinearLayout.LayoutParams params = LayoutParamsUtils.newInstance(mContext).getLinearLayoutParams(
							android.view.ViewGroup.LayoutParams.MATCH_PARENT,
							android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            mTitleBarView.setLayoutParams(params);
		}
        mTitleBarView.init(tbs, title);
        mTitleBarView.setOnTopbarListener(this);
	}

	/**
	 * 初始化更改布局的标题栏
	 * 
	 * @param tbs
	 * @param ls
	 * @param title
	 */
	protected <T> T initTitle(TopBarStyle tbs, LayoutStyle ls, int layoutId, CharSequence title) {
		initTitle(tbs, title);
		View v = mTitleBarView.setLayout(layoutId, ls);
		if (layoutId == R.layout.title_bar_text) {
			setTextVal((TextView) (v.findViewById(R.id.id_model_titlebar_text)));
		}
		return (T) v;
	}

	/**
	 * 设置按钮文字
	 * 
	 * @param v
	 */
	protected void setTextVal(TextView v) {
	}

	public CommonTitleBarView getTitleBarView() {
		return mTitleBarView;
	}


	protected void showToast(String text) {
		ToastUtils.showLongToastSafe(this, text);
	}

	@SuppressWarnings("rawtypes")
	protected void startActivity(Class cls) {
		mIntent = new Intent(mContext, cls);
		startActivity(mIntent);
	}

	@SuppressWarnings("rawtypes")
	protected void startActivity(Class cls, String key, Serializable obj) {
		mIntent = new Intent(mContext, cls);
		Bundle bundle = new Bundle();
		bundle.putSerializable(key, obj);
		mIntent.putExtras(bundle);
		startActivity(mIntent);
	}

	/**
	 * 启动线程
	 * 
	 * @param runnable
	 */
	protected void startThread(Runnable runnable) {
		new Thread(runnable).start();
	}

	/**
	 * 打Log ShowLog
	 * 
	 * @return void
	 * @throws
	 */
	protected void showLog(String msg) {
		LogUtils.e(getClass(), msg);
	}

	/**
	 * 销毁
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
        ActivityObjects.getInstance().removeActivity(this);
	}

	@Override
	public void onClick(View v) {
		clickbyId(v.getId());
	}

	protected void clickbyId(int id) {
	}

	/**
	 * 设置Value
	 * 
	 * @param viewId
	 * @param val
	 */
	protected void setValue(int viewId, Object val) {
		View view = findViewById(viewId);
        mViewUtils.setValue(view, val);
	}

    @Override
    public boolean onLeftLayoutClick(View v) {
        return true;
    }

    @Override
    public void onRightLayoutClick(View v) {

    }

    @Override
	public void onCenterLayoutClick(View v) {
	}

    public SystemBarTintManager getTintManager() {
        return mTintManager;
    }
}